<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page  language="java" import="java.io.*"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%!
//获取文档名称
public String GetFileName(String filepath) {
	String filename=null;
    if (filepath != "") {
        String[] names = filepath.split("\\\\");
		filename=names[names.length - 1];
    }
	  return filename;
}
%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setCharacterEncoding("UTF-8");
%>
<%  
  List  fileItems=null;
  String  uploadpath=null;
  String fileName = request.getParameter("filename");
  uploadpath=this.getServletContext().getRealPath("/")+"OfficeFile\\";
  String myhead=request.getHeader("myheader");
  DiskFileItemFactory factory = new DiskFileItemFactory();
	// 设置最多只允许在内存中存储的数据,单位:字节
	factory.setSizeThreshold(4096);
	ServletFileUpload upload = new ServletFileUpload(factory);
	//设置允许用户上传文件大小,单位:字节
	upload.setSizeMax(1024*1024*4);
	System.out.println("开始保存文档······");
	try{fileItems=upload.parseRequest(request);}
	catch(FileUploadException e)
	{
		out.println("the max upload size is 4m,cheeck upload file size!");
		out.println(e.getMessage());
		e.printStackTrace();
		return;
	}
  try{
	Iterator iter = fileItems.iterator();
	while (iter.hasNext()) 
	{
		FileItem item = (FileItem) iter.next();
		
		if(item.getFieldName().equalsIgnoreCase("p1")||item.getFieldName().equalsIgnoreCase("p2")||item.getFieldName().equalsIgnoreCase("p3")||item.getFieldName().equalsIgnoreCase("FileUploadText"))
			{
				System.out.println(item.getString("utf-8").trim());
			}

		if(item.getFieldName().equalsIgnoreCase("upLoadFile")||item.getFieldName().equalsIgnoreCase("attachFile[]"))
		{	
		System.out.println("\r\n==============================================================\r\n"+item+"\r\n==============================================================\r\n");
			File  fil=null;
			if(item.getFieldName().equalsIgnoreCase("upLoadFile")){
				fil=new File(uploadpath+item.getName());    //控件提交的文档保存路径
			}else if(item.getFieldName().equalsIgnoreCase("attachFile[]")){
				fil=new File(uploadpath+GetFileName(item.getName()));  //控件提交的附件保存路径
			}
			item.write(fil);
		}
	}
	System.out.println("保存文档结束······");
	out.print("保存成功");
	}catch(Exception e){
	 System.out.println("保存失败：" + e);	 
	 out.print("保存失败");
	 	}
	 %>
